package com.lintcode.solution;

import com.lintcode.solution.entity.TreeNode;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


/**
 * 1-7
 */
public class Solution {
    static int index = 0;
    
    public static void main(String[] agrs) {
        char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
        solution_8(str, 3);
    }
    
    private static int solution_1(int a, int b) {
        return (a ^ b) + ((a & b) << 1);
    }
    
    private static long solution_2(long n) {
        long count = 0;
        long temp = n / 5;
        while (temp != 0) {
            count += temp;
            temp /= 5;
        }
        return count;
    }
    
    private static void test() {
        for (int j = 5; j <= 500; j += 5) {
            BigInteger result = new BigInteger("1");
            for (int i = 2; i <= j; i++) {
                result = result.multiply(new BigInteger(i + ""));
            }
            String[] strings = result.toString().split("[1-9]");
            System.out.println(j + ":" + strings[ strings.length - 1 ].length());
        }
    }
    
    private static int solution_3(int k, int n) {
        String str = k + "";
        String temp;
        int count = 0;
        for (int i = 0; i <= n; i++) {
            temp = i + "";
            if (temp.contains(str)) {
                int index = 0;
                while (index != - 1) {
                    index = temp.indexOf(str, index);
                    if (index != - 1) {
                        count++;
                        index++;
                    }
                }
            }
        }
        return count;
    }
    
    public static int solution_4(int n) {
        int[] nums = new int[ n + 1 ];
        int p2, p3, p5;
        p2 = p3 = p5 = 0;
        nums[ 0 ] = 1;
        for (int i = 1; i < n; i++) {
            nums[ i ] = Math.min(Math.min(nums[ p2 ] * 2, nums[ p3 ] * 3), Math.min(nums[ p2 ] * 2, nums[ p5 ] * 5));
            if (nums[ i ] == nums[ p2 ] * 2) {
                ++ p2;
            }
            if (nums[ i ] == nums[ p3 ] * 3) {
                ++ p3;
            }
            if (nums[ i ] == nums[ p5 ] * 5) {
                ++ p5;
            }
        }
        return nums[ n - 1 ];
    }
    
    private static boolean isUglyNum(int n) {
        int result = n;
        while (result != 1) {
            if (result % 5 == 0) {
                result /= 5;
                continue;
            }
            if (result % 3 == 0) {
                result /= 3;
                continue;
            }
            if (result % 2 == 0) {
                result /= 2;
                continue;
            }
            break;
        }
        return result == 1;
    }
    
    public static int solution_5(int k, int[] nums) {
        Arrays.sort(nums);
        return nums[ nums.length - k ];
    }
    
    public static int[] solution_6(int[] A, int[] B) {
        List<Integer> set = new ArrayList<>();
        for (int i = 0; i < A.length; i++) {
            set.add(A[ i ]);
        }
        for (int i = 0; i < B.length; i++) {
            set.add(B[ i ]);
        }
        Collections.sort(set);
        int[] arr = new int[ set.size() ];
        int i = 0;
        for (Integer integer : set) {
            arr[ i++ ] = integer;
        }
        return arr;
    }
    
    public static String serialize(TreeNode root) {
        
        StringBuilder tree = new StringBuilder();
        Map<Integer, String> map = new TreeMap<>();
        int floor = traverse(root, map, 0, 1);
        int index = 0;
        for (int i = 0; i <= floor; i++) {
            index += 1 << i;
        }
        StringBuilder temp = new StringBuilder();
        String val;
        tree.append("{");
        for (int i = 1; i < index; i++) {
            val = map.get(i);
            if (val == null) {
                int a = i % 2 == 0 ? i / 2 : (i - 1) / 2;
                String pre = map.get(a);
                if (pre != null) {
                    if (i == 1) {
                        temp.append("#");
                    } else {
                        temp.append(",").append("#");
                    }
                }
            } else {
                if (i == 1) {
                    tree.append(temp.toString()).append(val);
                } else {
                    tree.append(temp.toString()).append(",").append(val);
                }
                temp = new StringBuilder();
            }
        }
        tree.append("}");
        return tree.toString();
    }
    
    public static int traverse(TreeNode node, Map<Integer, String> map, int floor, int index) {
        if (node == null) {
            return floor;
        }
        map.put(index, node.val + "");
        return Math.max(traverse(node.left, map, floor + 1, index * 2), traverse(node.right, map,
                floor + 1, index * 2 + 1));
    }
    
    public static TreeNode deserialize(String data) {
        String treeStr = data.substring(1, data.length() - 1);
        if ("".equals(treeStr)) {
            return null;
        }
        if (! treeStr.contains(",")) {
            return new TreeNode(Integer.valueOf(treeStr));
        }
        String[] arr = treeStr.split(",");
        List<TreeNode> t1 = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            if (arr[ i ].equals("#")) {
                arr[ i ] = Integer.MAX_VALUE + "";
            }
            int temp = Integer.valueOf(arr[ i ]);
            t1.add(new TreeNode(temp));//添加树结点
        }
        TreeNode root = t1.get(0);
        for (int i = 0, j = 1; j < t1.size(); i++) {
            
            
            TreeNode p = t1.get(i);
            if (p.val == Integer.MAX_VALUE) {
                continue;
            }
            //一定要检查j不要越界
            if (j < t1.size()) {
                if (t1.get(j).val == Integer.MAX_VALUE) {
                    j++;//是'#'不用管，默认null
                } else {
                    p.left = t1.get(j++);//左节点
                }
            }
            //同理
            if (j < t1.size()) {
                if (t1.get(j).val == Integer.MAX_VALUE) {
                    j++;
                } else {
                    p.right = t1.get(j++);//又节点
                }
            }
        }
        
        return root;
    }
    
    public static void solution_8(char[] str, int offset) {
        if (offset == 0 || (str == null || str.length == 0)) {
            return;
        }
        char temp1;
        int index = Math.min(offset, str.length);
        for (int j = 1; j <= index; j++) {
            temp1 = str[ str.length - 1 ];
            for (int i = str.length - 1; i >= 1; i--) {
                str[ i ] = str[ i - 1 ];
            }
            str[ 0 ] = temp1;
        }
        
        System.out.println(new String(str));
    }
    
    
}
